package com.orangelife.mobile.myhouse.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.jump.LocationJumper;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.StringUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.LocationBean;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 申请认证的第二步(非产权人)
 * <p/>
 * Created by wgf on 2016/6/22.
 *
 * @author wgf
 * @since v3.8.1
 */
public class HouseCert2Activity extends OrangeLifeBaseActivity {
    @Bind(R.id.rlSelectBuilding)
    RelativeLayout rlSelectBuilding;
    @Bind(R.id.rlSelectRoom)
    RelativeLayout rlSelectRoom;
    @Bind(R.id.ivJumpBuilding)
    ImageView ivJumpBuilding;
    @Bind(R.id.ivJumpRoom)
    ImageView ivJumpRoom;
    @Bind(R.id.etOwnerName)
    EditText etOwnerName;
    @Bind(R.id.etOwnerTel)
    EditText etOwnerTel;
    @Bind(R.id.btnCommit)
    Button btnCommit;
    @Bind(R.id.btnCancel)
    Button btnCancel;
    @Bind(R.id.tvBuilding)
    TextView tvBuilding;
    @Bind(R.id.tvRoomNum)
    TextView tvRoomNum;
    @Bind(R.id.layOwnerName)
    RelativeLayout layOwnerName;
    @Bind(R.id.layOwnerPhone)
    RelativeLayout layOwnerPhone;
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    private String commID;
    private String buildingID = "";
    private String apartmentID = "";
    private String currentType;
    private String applicationID;
    private boolean mIsFromWeb;

    private final String MODULE_NAME="/IA/UPOWriteIAInfo";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_house_cert2);
        ButterKnife.bind(this);
        getIntentInfo();
        initView();

    }

    private void initView() {
        tvTitle.setText(getString(R.string.identity));
    }

    /**
     * 数据采集体不要删除
     * @return
     */
    public Map<String,Object> getDataTrack(){
        Map<String,Object> map=new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        return map;
    }

    private void getIntentInfo() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            applicationID = bundle.getString("applicationID");
            commID = bundle.getString("commID");
            mIsFromWeb = "0".equals(bundle.getString("isFromWeb"));
        }
    }

    private void requestCommitCert() {
        String hostName = etOwnerName.getText().toString();
        String hostTel = etOwnerTel.getText().toString();
        if (!validate(hostName, hostTel, apartmentID)) {
            return;
        }
        showDialog(this);
        HouseHttpBiz.commitTenantCert(applicationID, apartmentID, hostName, hostTel, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                closeDialog();
                if (HouseHttpBiz.handleTenantCert(jsonObject, HouseCert2Activity.this, null)) {
                    if (mIsFromWeb) {
                        HouseBiz.jumpBackToWeb(HouseCert2Activity.this, true);
                    } else {
                        jumpToCode();
                    }
                }
                if(mIsFromWeb){
                    HouseBiz.jumpBackToWeb(HouseCert2Activity.this, false);
                }
            }
        });
    }

    /** 跳转到 */
    private void jumpToCode() {
        HouseJumper.jumpToHouseCode(this, applicationID);
    }

    private boolean validate(String hostName, String hostTel, String apartmentID) {
        if ("".equals(hostName.trim())) {
            ToastHelper.getInstance()._toast("产权人名字不能为空");
            return false;
        }
        if ("".equals(hostTel.trim())) {
            ToastHelper.getInstance()._toast("产权人电话不能为空");
            return false;
        }
        if (StringUtil.isBlank(apartmentID)) {
            ToastHelper.getInstance()._toast("请先选择房号");
            return false;
        }
        return true;
    }

    @OnClick({R.id.ib_title_left, R.id.rlSelectBuilding, R.id.rlSelectRoom, R.id.btnCancel, R.id.btnCommit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_title_left:
                finish();
                break;
            case R.id.iv_title_right:
                break;
            case R.id.rlSelectBuilding:
                if (commID == null) {
                    ToastHelper.getInstance().displayToastWithQuickClose("请先选择小区");
                } else {
                    currentType = SelectHouseLocationActivity.TYPE_BUILDING;
                    LocationJumper.jumpToSelectLocation(HouseCert2Activity.this, SelectHouseLocationActivity.TYPE_BUILDING, commID);
                    resetSelected();
                }
                break;
            case R.id.rlSelectRoom:
                String roomNum = hasSelectPreValue(SelectHouseLocationActivity.TYPE_ROOM_NUM);
                if (roomNum == null) {
                    ToastHelper.getInstance().displayToastWithQuickClose("请先选择楼栋");
                } else {
                    currentType = SelectHouseLocationActivity.TYPE_ROOM_NUM;
                    LocationJumper.jumpToSelectLocation(HouseCert2Activity.this, SelectHouseLocationActivity.TYPE_ROOM_NUM, roomNum);
                    resetSelected();
                }
                break;
            case R.id.btnCommit:
                dataTrackClick("btnSubmit",null);
                requestCommitCert();
                break;
            case R.id.btnCancel:
                IntentHelper.getInstances().getIntent(HouseCert2Activity.this, HouseListActivity.class);
                break;
        }

    }

    private String hasSelectPreValue(String type) {
        String building = tvBuilding.getText().toString();
        if (type.equals(SelectHouseLocationActivity.TYPE_ROOM_NUM) && !building.equals(HouseCertActivity.PLEASE_SELECT) && !"".equals(buildingID)) {
            return buildingID;
        }
        return null;
    }

    private void resetSelected() {
        if (currentType == null) {
            return;
        }
        if (currentType.equals(SelectHouseLocationActivity.TYPE_BUILDING)) {
            tvRoomNum.setText(HouseCertActivity.PLEASE_SELECT);
            apartmentID = "";
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        Bundle bundle = data.getExtras();
        if (bundle == null) {
            return;
        }
        LocationBean lb = (LocationBean) bundle.getSerializable(SelectHouseLocationActivity.INTENT_RESULT);
        setLocationInfo(lb);
    }

    private void setLocationInfo(LocationBean lb) {
        if (lb == null || currentType == null) {/*currentType可能是空的*/
            return;
        }
        if (SelectHouseLocationActivity.TYPE_BUILDING.equals(currentType)) {
            tvBuilding.setText(lb.getBuildingName());
            buildingID = lb.getBuildingID();
        } else {
            tvRoomNum.setText(lb.getApartmentName());
            apartmentID = lb.getApartmentID();
        }
    }

}
